<!DOCTYPE html>
<html lang="en" style="height: 100%">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>填写建团信息</title>
</head>
<script src="http://g.tbcdn.cn/mtb/lib-flexible/0.3.4/??flexible_css.js,flexible.js"></script>

<link rel="stylesheet" href="/static/css/weui.min.css">
<script src="/static/js/zepto.min.js"></script>
<style>
    .weui-cells .weui-cell {
        height: 1rem;
        font-size: 0.38rem;
    }

    .weui-cell .weui-cell__hd .weui-label {
        width: 2.4rem;
    }

    select {
        direction: rtl;
    }
</style>
<body style="background-color: #f5f5f5;height: 100%">
<div class="container">
    <div class="weui-cells weui-cells_form">
        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">导游姓名</label></div>
            <div class="weui-cell__bd">
                <input id="guide_name" class="weui-input" type="text" placeholder="请输入导游姓名" style="text-align: right"/>
            </div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">团队名称</label></div>
            <div class="weui-cell__bd">
                <input id="team_name" class="weui-input" type="text" placeholder="请输入团队名称" style="text-align: right"/>
            </div>
        </div>
        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">团队人数</label></div>
            <div class="weui-cell__bd">
                <input id="team_number" class="weui-input" type="number" placeholder="请输入团队人数"
                       style="text-align: right"/>
            </div>
        </div>

        <div class="weui-cell weui-cell_select" style="padding: 10px 15px">
            <div class="weui-cell__hd"><label class="weui-label">团队性质1</label></div>
            <div class="weui-cell__bd ">
                <select class="weui-select" id="team_nature_one">

                </select>
            </div>
        </div>

        <div class="weui-cell weui-cell_select" style="padding: 10px 15px">
            <div class="weui-cell__hd"><label class="weui-label">团队性质2</label></div>
            <div class="weui-cell__bd ">
                <select class="weui-select" id="team_nature_two">

                </select>
            </div>
        </div>
        <div class="weui-cell weui-cell_select" style="padding: 10px 15px">
            <div class="weui-cell__hd"><label class="weui-label">团队行程</label></div>
            <div class="weui-cell__bd ">
                <select class="weui-select" id="team_travel">

                </select>
            </div>
        </div>

        <div class="weui-cells__title">游览景点（多选）</div>
        <div class="weui-cells weui-cells_checkbox" id="tourist_view">

        </div>

        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">全陪姓名</label></div>
            <div class="weui-cell__bd">
                <input id="full_guide_name" class="weui-input" type="text" placeholder="请输入全陪姓名"
                       style="text-align: right"/>
            </div>
        </div>

        <div class="weui-cell">
            <div class="weui-cell__hd"><label class="weui-label">手机号码</label></div>
            <div class="weui-cell__bd">
                <input id="phone_number" class="weui-input" type="text" placeholder="请输入手机号码"
                       style="text-align: right"/>
            </div>
        </div>

        <div class="weui-cells__title">游客需求</div>
        <div class="weui-cells weui-cells_form">
            <div class="weui-cell" style="height: 2rem">
                <div class="weui-cell__bd">
                    <textarea class="weui-textarea" placeholder="请输入游客需求" rows="3" id="tourist_demand"></textarea>
                </div>
            </div>
        </div>


        <div class="weui-btn-area">
            <a class="weui-btn weui-btn_primary" style="
             font-size: 0.38rem;height:1.2rem;line-height:1.2rem;background-color: #6eb249;
            border-radius: 0.6rem;" href="javascript:" onclick="submitData()">确定</a>
        </div>
        <input id="teamView" type="hidden" value="<$ teamView $>">
        <input id="teamTravel" type="hidden" value="<$ teamTravel $>">
        <input id="teamNatureOne" type="hidden" value="<$ teamNatureOne $>">
        <input id="teamNatureTwo" type="hidden" value="<$ teamNatureTwo $>">
    </div>
</div>
</body>
<script>

  function initSelect() {
    if ($("#teamView").val()) {
      var json = JSON.parse($("#teamView").val());
      var html = "";
      for (var i = 0; i < json.length; i++) {
        html += ' <label class="weui-cell weui-check__label" for="' + json[i].type_code + '">' +
          '      <div class="weui-cell__hd">' +
          '      <input type="checkbox" class="weui-check" name="checkbox1" id="' + json[i].type_code + '">' +
          '      <i class="weui-icon-checked"></i>' +
          '      </div>' +
          '      <div class="weui-cell__bd">' +
          '      <p>' + json[i].name + '</p>' +
          '      </div>' +
          '      </label>'
      }
      $("#tourist_view").html(html);

    }

    if ($("#teamNatureOne").val()) {
      var json = JSON.parse($("#teamNatureOne").val());
      var html = "";
      for (var i = 0; i < json.length; i++) {
        html += '<option value="' + json[i].type_code + '">' + json[i].name + '</option>';
      }
      $("#team_nature_one").html(html);
    }
    if ($("#teamNatureTwo").val()) {
      var json = JSON.parse($("#teamNatureTwo").val());
      var html = "";
      for (var i = 0; i < json.length; i++) {
        html += '<option value="' + json[i].type_code + '">' + json[i].name + '</option>';
      }
      $("#team_nature_two").html(html);
    }
    if ($("#teamTravel").val()) {
      var json = JSON.parse($("#teamTravel").val());
      var html = "";
      for (var i = 0; i < json.length; i++) {
        html += '<option value="' + json[i].type_code + '">' + json[i].name + '</option>';
      }
      $("#team_travel").html(html);
    }
  }

  // 获取URL参数 e.g. map.html?sid=122134
  function getUrlParam(s, name) {
    var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
    if (reg.test(s))
      return unescape(RegExp.$2.replace(/\+/g, " "));
    return "";
  }

  var openid = '';


  $(function () {
    initSelect();
    openid = getUrlParam(location.href, "openid");

  });


  function submitData() {
    var s = $("#tourist_view").find("input[type=checkbox]");
    var tourist_viewA = [];
    for (var index = 0; index < s.length; index++) {
      if (s[index].checked) {
        tourist_viewA.push(s[index].id)
      }
    }
    var guide_name = $("#guide_name").val();
    var team_name = $("#team_name").val();
    var team_number = $("#team_number").val();
    var full_guide_name = $("#full_guide_name").val();
    var phone_number = $("#phone_number").val();
    var tourist_demand = $("#tourist_demand").val();

    var team_nature_two = $("#team_nature_two").val();
    var team_nature_one = $("#team_nature_one").val();
    var team_travel = $("#team_travel").val();
    var tourist_view = tourist_viewA.toString();

    if (!guide_name) {
      alert("请先输入导游姓名");
      return
    }
    if (!team_name) {
      alert("请先输入团队名称");
      return
    }
    if (!team_number) {
      alert("请先输入团队人数");
      return
    }
    if (!full_guide_name) {
      alert("请先输入全陪姓名");
      return
    }
    if(team_nature_one==2){
      if (!phone_number || phone_number.trim().length !== 11) {
        alert("请先输入11位手机号码");
        return
      }
    }

    var postData = {
      open_id: openid,
      guide_name: guide_name,
      team_name: team_name,
      team_number: team_number,
      full_guide_name: full_guide_name,
      phone_number: phone_number,
      team_nature_two: team_nature_two,
      team_nature_one: team_nature_one,
      tourist_view: tourist_view,
      team_travel: team_travel,
      tourist_demand: tourist_demand
    };
    $.post('/weixin/postTeam', postData, function (response) {
      if (response.errno == 0) {
        alert(response.errmsg);
        history.go(-1);
      } else {
        alert(response.errmsg);
      }
    })
  }
</script>

</html>